<template>
  <div>
    <Spin fix size="large">
      <Icon type="ios-loading" size="198" class="demo-spin-icon-load"></Icon>
      <div>
        <h4 v-text="initText"></h4>
      </div>
    </Spin>
  </div>
</template>

<script>
import Qs from "qs";
import { Message, Spin, Icon } from "view-design";
import appService from "@/utils/appService";
import { getAuthApi } from "@/api/auth";
export default {
  components: {
    Spin,
    Icon,
  },
  data() {
    return {
      initText: '初始化...'
    };
  },
  async mounted() {
    let windowQuery = window.location.search;
    let query = Qs.parse(windowQuery, { ignoreQueryPrefix: true });
    const clientId = query.clientId;
    const token = query.token;
    const tokenName = query.tokenName;
    console.log('clientId:', clientId)
    console.log('token:', token)
    console.log('tokenName:', tokenName)
    if (token) {
      const userInfoCache = {
        jwt: token,
        clientId: clientId,
        token: token
      }
      // 1、设置token
      appService.saveUserInfo(userInfoCache)
      // 2、获取用户信息
      const authInfo = await getAuthApi()
      console.log('查询登录用户信息返回：', authInfo)
      if (authInfo.status === 200 && authInfo.data) {
        const { code, data } = authInfo.data
        if (code === 200 && data) {
          const { menuNodeList, resourceList, userInfo } = data
          const newUserInfoCache = {
            ...userInfoCache,
            ...userInfo,
            menuNodeList: menuNodeList || [],
            resourceList: resourceList || [],
          }
          appService.saveUserInfo(newUserInfoCache)
          this.initText = '初始化完成!'
          this.$Message.success("初始化完成！");
          window.setTimeout(() => {
            this.$router.push("/");
          }, 1000);
          return
        }
      }
      this.$Message.error("获取用户信息异常，请联系管理员处理！");
      window.setTimeout(() => {
        this.$router.push("/signin");
      }, 3000);
    } else {
      this.$Message.error("无效授权码,3秒后跳转到登录页");
      window.setTimeout(() => {
        this.$router.push("/signin");
      }, 3000);
    }
  },
  // async mounted() {
  //   let windowQuery = window.location.search;
  //   let query = Qs.parse(windowQuery, { ignoreQueryPrefix: true });
  //   if (query.code) {
  //     let response = await appService.getRequest(
  //       `${config.serverProxy}${config.authServicePath}/auth/oauth2/callback`,
  //       query
  //     );
  //     let userInfo = response.data.data;
  //     if (!userInfo.jwt) {
  //       userInfo.jwt = userInfo.token;
  //     }
  //     appService.saveUserInfo(userInfo);
  //     if (response.status === 200 && response.data.errorCode === 0) {
  //       let roleRequest = await appService.getRequest(
  //         `${config.serverProxy}${config.authServicePath}/role/getMyAllRoles`
  //       );
  //       let permissionRequest = await appService.getRequest(
  //         `${config.serverProxy}${config.authServicePath}/permission/getMyAllPermissions`
  //       );
  //       if (roleRequest.status === 200 && roleRequest.data.errorCode === 0) {
  //         userInfo.roles = roleRequest.data.data.allRoleCodes;
  //       }
  //       if (
  //         permissionRequest.status === 200 &&
  //         permissionRequest.data.errorCode === 0
  //       ) {
  //         userInfo.permissions = permissionRequest.data.data.allPermissionCodes;
  //       }
  //       appService.saveUserInfo(userInfo);
  //     }
  //     this.$router.push("/");
  //   } else {
  //     this.$Message.error("无效授权码,3秒后跳转到登录页");
  //     window.setTimeout(() => {
  //       this.$router.push("/signin");
  //     }, 3000);
  //   }
  // },
};
</script>

<style>
.demo-spin-icon-load {
  animation: ani-demo-spin 1s linear infinite;
}

@keyframes ani-demo-spin {
  from {
    transform: rotate(0deg);
  }

  50% {
    transform: rotate(180deg);
  }

  to {
    transform: rotate(360deg);
  }
}

.demo-spin-col {
  height: 100px;
  position: relative;
  border: 1px solid #eee;
}
</style>
